package com.jy.adscm.hospital.service.impl;

import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.mapper.JyHospitalBaseMapper;
import com.jy.adscm.hospital.service.JyHospitalBaseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.hospital.support.HospitalSelectSupport;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.task.bean.JyJwdTask;
import com.jy.adscm.task.mapper.JyJwdTaskMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 医院基础信息表 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-01-16
 */
@Service
public class JyHospitalBaseServiceImpl extends ServiceImpl<JyHospitalBaseMapper, JyHospitalBase> implements JyHospitalBaseService {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    JyJwdTaskMapper jyJwdTaskMapper;
    @Autowired
    JyHospitalBaseMapper jyHospitalBaseMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    DealerMapper dealerMapper;
    @Autowired
    JyUserMapper jyUserMapper;

    @Override
    public boolean insert(JyHospitalBase entity) {

        boolean flag = super.insert(entity);

        JyJwdTask task = new JyJwdTask();
        task.setTableId(entity.getId());
        task.setTableType(1);//1代表医院信息，2代表经销商信息

        jyJwdTaskMapper.insert(task);

        return flag;
    }


    @Override
    public List<JyHospitalBase> queryAllHospitalBaseList() {

        logger.info("开始查询所有医院基本信息列表，只返回列表需要展示的那几个字段，和经纬度。。。");

//        return jyHospitalBaseMapper.queryAllHospitalBaseList();
        return jyHospitalBaseMapper.queryAllHospitalBaseList1();

    }

    @Override
    public List<HospitalSelectSupport> queryAllHospitalList() {
        logger.info("开始查询所有医院信息，只有名称和ID。。。");
        return jyHospitalBaseMapper.queryAllHospitalList();
    }

    @Override
    public List<JyHospitalBase> queryHospitalListByLikeName(String name) {

        logger.info("根据字符串，模糊匹配医院名称，查询匹配到的医院列表。。。");
        if (name != null && !"".equals(name.trim())){

            name = AdscmBaseUtils.getInstance().addLikeStr(name);
            logger.info("匹配字符：" + name);
            return jyHospitalBaseMapper.queryHospitalListByLikeName(name);
        }else {
            return null;
        }

    }

    @Override
    public JyHospitalBase queryHospitalBaseById(Long id) {

        JyHospitalBase base = null;
        logger.info("查询[id="+id+"]的医院的基础信息详情。。。");

        if (id != null){
            base = jyHospitalBaseMapper.selectById(id);
            if (base != null){
                List<JyCode> hospital_type = jyCodeMapper.getJyCodeByCodeType("hospital_type");
                if (hospital_type != null && hospital_type.size()>0){
                    for (JyCode jyCode : hospital_type) {
                        long code = Long.parseLong(jyCode.getCode());

                        if (code == base.getHospitalType()){
                            base.setHospitalTypeDesc(jyCode.getName());
                        }
                    }
                }
//                JyCode jyCode = jyCodeMapper.selectById(base.getHospitalType());
//                base.setHospitalTypeDesc(jyCode.getName());

                hospital_type = jyCodeMapper.getJyCodeByCodeType("hospital_level");
                if (hospital_type != null && hospital_type.size()>0){
                    for (JyCode jyCode : hospital_type) {
                        long code = Long.parseLong(jyCode.getCode());

                        if (code == base.getHospitalLevel()){
                            base.setHospitalLevelDesc(jyCode.getName());
                        }
                    }
                }

//                jyCode = jyCodeMapper.selectById(base.getHospitalLevel());
//                base.setHospitalLevelDesc(jyCode.getName());

                Integer unitType = base.getInputUnitType();
                if (unitType == 1){
                    base.setInputUnitDesc("生产厂商");
                }else {
                    //获取经销商名称
                    Dealer dealer = dealerMapper.selectById(base.getInputUnit());
                    if (dealer != null){
                        base.setInputUnitDesc(dealer.getName());
                    }
                }

                Integer userId = base.getInputUserId();
                if (userId != null){
                    JyUser user = jyUserMapper.selectById(userId);
                    if (user != null){
                        base.setInputUserName(user.getName());
                    }
                }

            }
        }
        return base;
    }


    @Override
    public List<JyCode> queryHospitalTypeLevel(int type) {

        List<JyCode> jyCodes = null;
        if(type == 1){
            //获取医院类别
            logger.info("查询医院类别。。。");
            jyCodes = jyCodeMapper.getJyCodeByCodeType("hospital_type");
        }else {
            logger.info("查询医院级别。。。");
            jyCodes = jyCodeMapper.getJyCodeByCodeType("hospital_level");
        }

        return jyCodes;
    }
}
